define(function(require, exports, module) {

	var $ = require("jquery1.7");

	function maskElement(element, label) {

		if (element.data("_mask_timeout") !== undefined) {
			clearTimeout(element.data("_mask_timeout"));
			element.removeData("_mask_timeout");
		}

		if (element.hasClass("masked")) {
			unmaskElement(element);
		}

		if (element.css("position") == "static") {
			element.addClass("masked-relative");
		}

		element.addClass("masked");

		var maskDiv = $('<div class="loadmask"></div>');

		if (navigator.userAgent.toLowerCase().indexOf("msie") > -1) {
			maskDiv.height(element.height()
					+ parseInt(element.css("padding-top"))
					+ parseInt(element.css("padding-bottom")));
			maskDiv.width(element.width()
					+ parseInt(element.css("padding-left"))
					+ parseInt(element.css("padding-right")));
		}

		if (navigator.userAgent.toLowerCase().indexOf("msie 6") > -1) {
			element.find("select").addClass("masked-hidden");
		}

		element.append(maskDiv);

		if (label !== undefined) {
			var maskMsgDiv = $('<div class="loadmask-msg" style="display:none;"></div>');
			maskMsgDiv.append('<div>' + label + '</div>');
			element.append(maskMsgDiv);
			maskMsgDiv
					.css(
							"top",
							Math
									.round(element.height()
											/ 2
											- (maskMsgDiv.height()
													- parseInt(maskMsgDiv
															.css("padding-top")) - parseInt(maskMsgDiv
													.css("padding-bottom")))
											/ 2 - 10)
									+ "px");
			maskMsgDiv
					.css(
							"left",
							Math
									.round(element.width()
											/ 2
											- (maskMsgDiv.width()
													- parseInt(maskMsgDiv
															.css("padding-left")) - parseInt(maskMsgDiv
													.css("padding-right"))) / 2)
									+ "px");
			maskMsgDiv.show();
		}

	}

	function unmaskElement(element) {
		if (element.data("_mask_timeout") !== undefined) {
			clearTimeout(element.data("_mask_timeout"));
			element.removeData("_mask_timeout");
		}
		element.find(".loadmask-msg,.loadmask").remove();
		element.removeClass("masked");
		element.removeClass("masked-relative");
		element.find("select").removeClass("masked-hidden");
	}

	function Maks(maskContainer) {

		if (maskContainer == undefined)
			maskContainer = "body";

		this.container = $(maskContainer);
		
	}

	Maks.prototype.show = function(label, delay) {
		$(this.container).each(function() {
			if (delay !== undefined && delay > 0) {
				var element = $(this);
				element.data("_mask_timeout", setTimeout(function() {
					maskElement(element, label)
				}, delay));
			} else {
				maskElement($(this), label);
			}
		});

	}

	Maks.prototype.hide = function() {
		$(this.container).each(function() {
			unmaskElement($(this));
		});
	}

	Maks.prototype.isMasked = function() {
		return this.container.hasClass("masked");
	};

	module.exports = Maks;

});
